import java.util.*;

// 科室类
class Department {
    private String name;
    private List<Doctor> doctors;

    public Department(String name) {
        this.name = name;
        this.doctors = new ArrayList<>();
    }

    public String getName() {
        return name;
    }

    public List<Doctor> getDoctors() {
        return doctors;
    }

    public void addDoctor(Doctor doctor) {
        doctors.add(doctor);
    }

    public void removeDoctor(Doctor doctor) {
        doctors.remove(doctor);
    }
}

// 医生类
class Doctor {
    private String name;
    private Department department;
    private List<Schedule> schedules;
    private List<Appointment> appointments;

    public Doctor(String name, Department department) {
        this.name = name;
        this.department = department;
        this.schedules = new ArrayList<>();
        this.appointments = new ArrayList<>();
        department.addDoctor(this);
    }

    public String getName() {
        return name;
    }

    public Department getDepartment() {
        return department;
    }

    public void setDepartment(Department department) {
        this.department.getDoctors().remove(this);
        this.department = department;
        department.addDoctor(this);
    }

    public List<Schedule> getSchedules() {
        return schedules;
    }

    public void addSchedule(Schedule schedule) {
        schedules.add(schedule);
    }

    public List<Appointment> getAppointments() {
        return appointments;
    }

    public void addAppointment(Appointment appointment) {
        appointments.add(appointment);
    }
}

// 坐诊信息类
class Schedule {
    private Date date;
    private String timeSlot;
    private int availableSlots;

    public Schedule(Date date, String timeSlot, int availableSlots) {
        this.date = date;
        this.timeSlot = timeSlot;
        this.availableSlots = availableSlots;
    }

    public Date getDate() {
        return date;
    }

    public String getTimeSlot() {
        return timeSlot;
    }

    public int getAvailableSlots() {
        return availableSlots;
    }

    public void reduceAvailableSlots() {
        if (availableSlots > 0) {
            availableSlots--;
        }
    }
}

// 预约信息类
class Appointment {
    private Doctor doctor;
    private Date date;
    private String timeSlot;
    private String patientInfo;

    public Appointment(Doctor doctor, Date date, String timeSlot, String patientInfo) {
        this.doctor = doctor;
        this.date = date;
        this.timeSlot = timeSlot;
        this.patientInfo = patientInfo;
    }

    public Doctor getDoctor() {
        return doctor;
    }

    public Date getDate() {
        return date;
    }

    public String getTimeSlot() {
        return timeSlot;
    }

    public String getPatientInfo() {
        return patientInfo;
    }
}

// 医院管理系统类
class HospitalManagementSystem {
    private List<Department> departments;

    public HospitalManagementSystem() {
        this.departments = new ArrayList<>();
    }

    // 新增科室
    public void addDepartment(String name) {
        Department department = new Department(name);
        departments.add(department);
        System.out.println("科室 " + name + " 已添加。");
    }

    // 删除科室
    public void removeDepartment(String name) {
        for (Department department : departments) {
            if (department.getName().equals(name)) {
                if (department.getDoctors().isEmpty()) {
                    departments.remove(department);
                    System.out.println("科室 " + name + " 已删除。");
                } else {
                    System.out.println("该科室有医生，不能删除。");
                }
                return;
            }
        }
        System.out.println("未找到该科室。");
    }

    // 修改科室
    public void modifyDepartment(String oldName, String newName) {
        for (Department department : departments) {
            if (department.getName().equals(oldName)) {
                department = new Department(newName);
                System.out.println("科室名称已从 " + oldName + " 修改为 " + newName);
                return;
            }
        }
        System.out.println("未找到该科室。");
    }

    // 录入医生信息
    public void addDoctor(String doctorName, String departmentName) {
        for (Department department : departments) {
            if (department.getName().equals(departmentName)) {
                Doctor doctor = new Doctor(doctorName, department);
                System.out.println("医生 " + doctorName + " 已录入到 " + departmentName + " 科室。");
                return;
            }
        }
        System.out.println("未找到该科室。");
    }

    // 修改医生信息
    public void modifyDoctor(String doctorName, String newDepartmentName) {
        for (Department department : departments) {
            for (Doctor doctor : department.getDoctors()) {
                if (doctor.getName().equals(doctorName)) {
                    for (Department newDepartment : departments) {
                        if (newDepartment.getName().equals(newDepartmentName)) {
                            doctor.setDepartment(newDepartment);
                            System.out.println("医生 " + doctorName + " 已转移到 " + newDepartmentName + " 科室。");
                            return;
                        }
                    }
                    System.out.println("未找到新科室。");
                    return;
                }
            }
        }
        System.out.println("未找到该医生。");
    }

    // 设置坐诊信息
    public void setSchedule(String doctorName, Date date, String timeSlot, int availableSlots) {
        for (Department department : departments) {
            for (Doctor doctor : department.getDoctors()) {
                if (doctor.getName().equals(doctorName)) {
                    Schedule schedule = new Schedule(date, timeSlot, availableSlots);
                    doctor.addSchedule(schedule);
                    System.out.println("医生 " + doctorName + " 在 " + date + " " + timeSlot + " 的坐诊信息已设置。");
                    return;
                }
            }
        }
        System.out.println("未找到该医生。");
    }

    // 全部信息展示
    public void displayAllSchedules() {
        for (Department department : departments) {
            System.out.println("科室: " + department.getName());
            for (Doctor doctor : department.getDoctors()) {
                System.out.println("  医生: " + doctor.getName());
                for (Schedule schedule : doctor.getSchedules()) {
                    System.out.println("    日期: " + schedule.getDate() + " 时间段: " + schedule.getTimeSlot() + " 可预约数量: " + schedule.getAvailableSlots());
                }
            }
        }
    }

    // 预约功能
    public void makeAppointment(String departmentName, String doctorName, Date date, String timeSlot, String patientInfo) {
        for (Department department : departments) {
            if (department.getName().equals(departmentName)) {
                for (Doctor doctor : department.getDoctors()) {
                    if (doctor.getName().equals(doctorName)) {
                        for (Schedule schedule : doctor.getSchedules()) {
                            if (schedule.getDate().equals(date) && schedule.getTimeSlot().equals(timeSlot)) {
                                if (schedule.getAvailableSlots() > 0) {
                                    Appointment appointment = new Appointment(doctor, date, timeSlot, patientInfo);
                                    doctor.addAppointment(appointment);
                                    schedule.reduceAvailableSlots();
                                    System.out.println("预约成功。");
                                    return;
                                } else {
                                    System.out.println("该时间段已无预约名额。");
                                    return;
                                }
                            }
                        }
                        System.out.println("未找到该医生在该日期和时间段的坐诊信息。");
                        return;
                    }
                }
                System.out.println("未找到该医生。");
                return;
            }
        }
        System.out.println("未找到该科室。");
    }

    // 搜索功能
    public void searchDoctors(Date date, String timeSlot) {
        for (Department department : departments) {
            System.out.println("科室: " + department.getName());
            boolean found = false;
            for (Doctor doctor : department.getDoctors()) {
                for (Schedule schedule : doctor.getSchedules()) {
                    if (schedule.getDate().equals(date) && schedule.getTimeSlot().equals(timeSlot)) {
                        System.out.println("  医生: " + doctor.getName());
                        found = true;
                    }
                }
            }
            if (!found) {
                System.out.println("  无符合条件的医生。");
            }
        }
    }

    // 查询医生预约情况
    public void queryDoctorAppointments(String doctorName) {
        for (Department department : departments) {
            for (Doctor doctor : department.getDoctors()) {
                if (doctor.getName().equals(doctorName)) {
                    System.out.println("医生 " + doctorName + " 的预约情况:");
                    for (Appointment appointment : doctor.getAppointments()) {
                        System.out.println("  日期: " + appointment.getDate() + " 时间段: " + appointment.getTimeSlot() + " 患者信息: " + appointment.getPatientInfo());
                    }
                    return;
                }
            }
        }
        System.out.println("未找到该医生。");
    }
}

// 主类，用于测试
public class test {
    public static void main(String[] args) {
        HospitalManagementSystem system = new HospitalManagementSystem();
        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.println("\n请选择操作:");
            System.out.println("1. 新增科室");
            System.out.println("2. 删除科室");
            System.out.println("3. 修改科室");
            System.out.println("4. 录入医生信息");
            System.out.println("5. 修改医生信息");
            System.out.println("6. 设置坐诊信息");
            System.out.println("7. 全部信息展示");
            System.out.println("8. 预约功能");
            System.out.println("9. 搜索功能");
            System.out.println("10. 查询医生预约情况");
            System.out.println("0. 退出");

            int choice = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符

            switch (choice) {
                case 1:
                    System.out.println("请输入科室名称:");
                    String newDepartmentName = scanner.nextLine();
                    system.addDepartment(newDepartmentName);
                    break;
                case 2:
                    System.out.println("请输入要删除的科室名称:");
                    String removeDepartmentName = scanner.nextLine();
                    system.removeDepartment(removeDepartmentName);
                    break;
                case 3:
                    System.out.println("请输入原科室名称:");
                    String oldDepartmentName = scanner.nextLine();
                    System.out.println("请输入新科室名称:");
                    String newName = scanner.nextLine();
                    system.modifyDepartment(oldDepartmentName, newName);
                    break;
                case 4:
                    System.out.println("请输入医生姓名:");
                    String doctorName = scanner.nextLine();
                    System.out.println("请输入科室名称:");
                    String departmentName = scanner.nextLine();
                    system.addDoctor(doctorName, departmentName);
                    break;
                case 5:
                    System.out.println("请输入要修改信息的医生姓名:");
                    String modifyDoctorName = scanner.nextLine();
                    System.out.println("请输入新的科室名称:");
                    String newDepartment = scanner.nextLine();
                    system.modifyDoctor(modifyDoctorName, newDepartment);
                    break;
                case 6:
                    System.out.println("请输入医生姓名:");
                    String setScheduleDoctorName = scanner.nextLine();
                    System.out.println("请输入日期 (格式: yyyy-MM-dd):");
                    String dateStr = scanner.nextLine();
                    // 这里需要简单的日期解析逻辑，示例中省略
                    Date date = new Date();
                    System.out.println("请输入时间段 (如: 上午/下午):");
                    String timeSlot = scanner.nextLine();
                    System.out.println("请输入可预约数量:");
                    int availableSlots = scanner.nextInt();
                    scanner.nextLine(); // 消耗换行符
                    system.setSchedule(setScheduleDoctorName, date, timeSlot, availableSlots);
                    break;
                case 7:
                    system.displayAllSchedules();
                    break;
                case 8:
                    System.out.println("请输入科室名称:");
                    String appointmentDepartmentName = scanner.nextLine();
                    System.out.println("请输入医生姓名:");
                    String appointmentDoctorName = scanner.nextLine();
                    System.out.println("请输入日期 (格式: yyyy-MM-dd):");
                    String appointmentDateStr = scanner.nextLine();
                    // 这里需要简单的日期解析逻辑，示例中省略
                    Date appointmentDate = new Date();
                    System.out.println("请输入时间段 (如: 上午/下午):");
                    String appointmentTimeSlot = scanner.nextLine();
                    System.out.println("请输入患者信息:");
                    String patientInfo = scanner.nextLine();
                    system.makeAppointment(appointmentDepartmentName, appointmentDoctorName, appointmentDate, appointmentTimeSlot, patientInfo);
                    break;
                case 9:
                    System.out.println("请输入搜索日期 (格式: yyyy-MM-dd):");
                    String searchDateStr = scanner.nextLine();
                    // 这里需要简单的日期解析逻辑，示例中省略
                    Date searchDate = new Date();
                    System.out.println("请输入时间段 (如: 上午/下午):");
                    String searchTimeSlot = scanner.nextLine();
                    system.searchDoctors(searchDate, searchTimeSlot);
                    break;
                case 10:
                    System.out.println("请输入医生姓名:");
                    String queryDoctorName = scanner.nextLine();
                    system.queryDoctorAppointments(queryDoctorName);
                    break;
                case 0:
                    System.out.println("退出系统。");
                    scanner.close();
                    return;
                default:
                    System.out.println("无效的选择，请重新输入。");
            }
        }
    }
}    